package com.service.impl;

import com.bean.entity.Supplier;
import com.bean.vo.PageAssistant;
import com.bean.vo.SupplierVo;
import com.dao.SupplierMapper;
import com.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService{

    /* 自动注入 */
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public PageAssistant<SupplierVo> queryAll(PageAssistant<SupplierVo> pa,SupplierVo supplierVo) {
        //查询总记录数
        pa.setCount(supplierMapper.selectCount(supplierVo));
        //设置分页
        supplierVo.setPageSize(pa.getPageSize());
        supplierVo.setCurrPage(pa.getCurrPage());
        supplierVo.setStartCode(pa.getStartCode());
        //设置页面内容
        pa.setItems(supplierMapper.selectByPage(supplierVo));
        return pa;
    }

    @Override
    public SupplierVo query(Supplier supplier) throws Exception {
        //查询单个
        List<SupplierVo> supplierVos = supplierMapper.select(supplier);
        if(supplierVos.size() != 1){
            throw new Exception("抱歉，为查询到该用户!");
        }else{
            return supplierVos.get(0);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Supplier supplier) throws Exception {
        //供应商添加
        int i = supplierMapper.insert(supplier);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("抱歉，添加失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modify(SupplierVo supplierVo) throws Exception {
        //供应商修改
        int i = supplierMapper.update(supplierVo);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("抱歉，修改失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Supplier supplier) throws Exception {
        //供应商删除
        int i = supplierMapper.delete(supplier);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("抱歉，删除失败！");
        }
    }
}
